<?php

namespace catchAdmin\basics\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\basics\model\Rate as RateModel;
use catcher\CatchAuth;
use think\facade\Db;
use think\Response;

class Rate extends CatchController
{
    
    protected $rateModel;
    
    /**
     *
     * @time 2022/03/02 09:48
     * @param RateModel $rateModel
     * @return mixed
     */
    public function __construct(RateModel $rateModel)
    {
        $this->rateModel = $rateModel;
    }
    
    /**
     *
     * @time 2022/03/02 09:48
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function index(CatchAuth $auth)
    {
        $user = $auth->user();
        return CatchResponse::paginate($this->rateModel->getList($user['tenant_id']));
    }

    /**
     *
     * @time 2022/02/28 14:25
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function create(CatchAuth $auth,Request $request)
    {
        $params = $request->filterEmptyField()->post();
        $user = $auth->user();
        $params['tenant_id'] = $user['tenant_id'];
        return CatchResponse::success($this->rateModel->storeBy($params),"新建成功");
    }
    
    /**
     *
     * @time 2022/03/02 09:48
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function update(Request $request)
    {
        $params = $request->filterEmptyField()->post();
        return CatchResponse::success($this->rateModel->updateBy($params['id'], $params),"修改成功");
    }
    
    /**
     *
     * @time 2022/03/02 09:48
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function delete(Request $request)
    {
        $id = $request->post('id');
        return CatchResponse::success($this->rateModel->deleteBy($id),"删除成功");
    }
    /**
     * 查询客户信息
     * @time 2022/03/18 11:41
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function getRate(Request $request)
    {
        $where=[];
        $where[] = ['deleted_at','=',0];
        $date = date('Y-m-d');
        $where[] = ['start_date','<=',$date];
        $where[] = ['end_date','>=',$date];
        $data = $this->rateModel
                        ->withoutField('tenant_id,creator_id,updated_at,deleted_at,created_at')
                        ->where($where)
                        ->select()
                        ->each(function (&$item){
                            $item['source_currency_text'] = Db::table('db_currency')
                                ->where([['deleted_at','=',0],['id','=',$item['source_currency']]])
                                ->value('currency_num');
                            $item['target_currency_text'] = Db::table('db_currency')
                                ->where([['deleted_at','=',0],['id','=',$item['target_currency']]])
                                ->value('currency_num');
                        });
        return CatchResponse::success($data, '查询成功');
    }
}